<?php
namespace Org\Util;
class Duanxin{
	/**
	*云片网络发短信接口  
	*/
	protected $apikey="43561b65ab602a2bf1e1462b0cefd748";
	protected $mobile;
	protected $text="您的验证码是";
	protected $code;
	protected $company="【中防德邦】";
	protected $ch;

	public function __construct($mobile,$code) {
		$this->ch=curl_init();
		/* 设置验证方式 */
		curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded','charset=utf-8'));
		/* 设置返回结果为流 */
		curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
		/* 设置超时时间*/
		curl_setopt($this->ch, CURLOPT_TIMEOUT, 10);
		/* 设置通信方式 */
		curl_setopt($this->ch, CURLOPT_POST, 1);
		curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
		$this->code=$code;
		$this->mobile=$mobile;
		$this->text=$this->company.$code.'(本次获取的验证码）有效期为30分钟，如果非本人操作，请忽略此短信。';
		// echo 'this->code:'.$this->code.'===code:'.$code.'===text:'.$this->text;
	}
	/**
	 * 添加模板
	*/
	public function addtpl(){
		curl_setopt ($this->ch, CURLOPT_URL, 'https://sms.yunpian.com/v1/tpl/add.json');
		$data=array(
			'apikey'=>$this->apikey,
			'tpl_content'=>"【中防德邦】#code#(本次获取的验证码）有效期为30分钟，如果非本人操作，请忽略此短信。"
		);
		curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($data));
		$ret=curl_exec($this->ch);
		curl_close($this->ch);
		$ret=json_decode($ret,1);
		 dump($ret);die;
		return $ret;
	}




	//取得用户信息
	public function get_user(){
	    curl_setopt ($this->ch, CURLOPT_URL, 'https://sms.yunpian.com/v1/user/get.json');
	    curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query(array('apikey' => $this->apikey)));
	    $data=curl_exec($this->ch);
	    curl_close($this->ch);
	    $data=json_decode($data,1);
	    return $data;
	}
	// 发送短信
	// $data=array('text'=>$text,'apikey'=>$apikey,'mobile'=>$mobile);
	public function send(){
	    curl_setopt ($this->ch, CURLOPT_URL, 'https://sms.yunpian.com/v1/sms/send.json');
	    curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query(array('text'=>$this->text,'apikey'=>$this->apikey,'mobile'=>$this->mobile)));
	    $ret=curl_exec($this->ch);
	    curl_close($this->ch);
	    $ret=json_decode($ret,1);
	    // dump($ret);die;
	    return $ret;
	}
	// 发送模板短信
	// $data=array('tpl_id'=>'1','tpl_value'=>('#code#').'='.urlencode('1234').'&'.urlencode('#company#').'='.urlencode('欢乐行'),'apikey'=>$apikey,'mobile'=>$mobile);
	public function tpl_send(){
		$data=array(
			'tpl_id'=>'1',
			'tpl_value'=>('#code#').'='.urlencode($this->code).'&'.urlencode('#company#').'='.urlencode($this->company),
			'apikey'=>$this->apikey,
			'mobile'=>$this->mobile
			);
	    curl_setopt ($this->ch, CURLOPT_URL, 'https://sms.yunpian.com/v1/sms/tpl_send.json');
	    curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($data));
	    $ret=curl_exec($this->ch);
	    curl_close($this->ch);
	    $ret=json_decode($ret,1);
	    return $ret;
	}
	// 发送语音验证码
	// array('code'=>'9876','apikey'=>$apikey,'mobile'=>$mobile);
	public function voice_send(){
	    curl_setopt ($this->ch, CURLOPT_URL, 'http://voice.yunpian.com/v1/voice/send.json');
	    curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query(array('code'=>$this->code,'apikey'=>$this->apikey,'mobile'=>$this->mobile)));
	    $ret=curl_exec($this->ch);
	    curl_close($this->ch);
	    $ret=json_decode($ret,1);
	    return $ret;
	}

}